﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class SupplyWaterConnector : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			ViewPlan viewPlan = base.Doc.ActiveView as Autodesk.Revit.DB.ViewPlan;
			if (viewPlan == null || (int)viewPlan.ViewType != 1)
			{
				string text = "请在平面视图下运行该命令！";
				YJKMessageBox.Show(text);
				return Autodesk.Revit.UI.Result.Failed;
			}
			FormSupplyAutoConn formSupplyAutoConn = new FormSupplyAutoConn();
			try
			{
				for (;;)
				{
					DialogResult dialogResult = formSupplyAutoConn.ShowDialog();
					PlumbSystemAutoDesign plumbSystemAutoDesign = new PlumbSystemAutoDesign(base.UiDoc);
					if (dialogResult == DialogResult.Abort)
					{
						try
						{
							XYZ startPnt = base.Sel.PickPoint("请选择第一点……");
							XYZ endPnt = base.Sel.PickPoint("请选择第二点……");
							double angle = Convert.ToDouble(StringTools.CutOffEfficacious(plumbSystemAutoDesign.GetLineToXAngle(startPnt, endPnt).ToString(), 1));
							formSupplyAutoConn.SetAngle(angle);
						}
						catch (Exception)
						{
						}
					}
					if (dialogResult == DialogResult.OK)
					{
						SupplyWaterConn supplyWaterConn = new SupplyWaterConn();
						supplyWaterConn.ConnStyle = formSupplyAutoConn.GetDesignStyle();
						supplyWaterConn.BeDraw = formSupplyAutoConn.GetBeDrawPipe();
						supplyWaterConn.WaterMeter = formSupplyAutoConn.GetWaterMeter();
						supplyWaterConn.Angle = formSupplyAutoConn.GetAngle().ToString();
						supplyWaterConn.Diameter = formSupplyAutoConn.GetPipeDiameter().ToString();
						supplyWaterConn.MainPipeOffset = formSupplyAutoConn.GetPipeOffset().ToString();
						supplyWaterConn.ValveOffset = formSupplyAutoConn.GetValveOffset().ToString();
						supplyWaterConn.BIsParallel = formSupplyAutoConn.bIsParallel;
						Pipe pipe = null;
						Pipe pipe2 = null;
						List<Element> list;
						for (;;)
						{
							list = this.SelectingElements(formSupplyAutoConn.GetDesignStyle());
							if (formSupplyAutoConn.IsLinkBathe())
							{
								List<Element> collection = HYPlumbingAutoDesignNew.SelectingLinkElements(base.UiDoc, false, true);
								list.AddRange(collection);
							}
							string text = plumbSystemAutoDesign.CheckingElements(list, formSupplyAutoConn.GetDesignStyle(), ref pipe, ref pipe2);
							if (!(text != ""))
							{
								break;
							}
							YJKMessageBox.Information(text);
							RevitVersionFuncs.ClearSelection(base.Sel);
						}
						bool flag = true;
						this.tran = new Transaction(base.Doc, "给水自动连接");
						this.tran.Start();
						if (pipe != null)
						{
							SupplySystemDesign supplySystemDesign = new SupplySystemDesign(list, pipe, supplyWaterConn, base.UiApp);
							supplySystemDesign.IsHorFirst = formSupplyAutoConn.bIsParallel;
							supplySystemDesign.AutoConnector();
							if (!supplySystemDesign.checkPVDistance)
							{
								flag = false;
							}
						}
						if (flag && pipe2 != null)
						{
							SupplySystemDesign supplySystemDesign2 = new SupplySystemDesign(list, pipe2, supplyWaterConn, base.UiApp);
							supplySystemDesign2.IsHorFirst = formSupplyAutoConn.bIsParallel;
							supplySystemDesign2.AutoConnector();
							if (!supplySystemDesign2.checkPVDistance)
							{
								flag = false;
							}
						}
						if (flag)
						{
							break;
						}
						dialogResult = DialogResult.OK;
						this.tran.RollBack();
					}
					if (dialogResult == DialogResult.Cancel)
					{
						goto Block_14;
					}
				}
				YJKMessageBox.Information("连接完成");
				this.tran.Commit();
				formSupplyAutoConn.Close();
				goto IL_272;
				Block_14:
				formSupplyAutoConn.Close();
				IL_272:;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
				return Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (this.tran != null)
				{
					TransactionStatus status = this.tran.GetStatus();
					if ((int)status == 1)
					{
						this.tran.Commit();
					}
					else if ((int)status == 5)
					{
						this.tran.RollBack();
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private List<Element> SelectingElements(bool beRiser)
		{
			string text = "请选择给水系统自动连接需要的实体：一根或两根（热水、冷水）水管立管、若干卫生设备……";
			ISelectionFilter selectionFilter = new AutoDesignRiserPipe(false);
			if (!beRiser)
			{
				text = "请选择给水系统自动连接需要的实体：一根或两根（热水、冷水）横管、若干卫生设备……";
				selectionFilter = new AutoDesignCommonPipe(false);
			}
			List<Element> list = null;
			new PlumbSystemAutoDesign(base.Doc);
			list = new List<Element>();
			if (base.Sel.GetElementIds().Count > 0)
			{
				IEnumerator<ElementId> enumerator = base.Sel.GetElementIds().GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						ElementId elementId = enumerator.Current;
						list.Add(base.Doc.GetElementById(elementId));
					}
					return list;
				}
			}
			foreach (Reference reference in base.Sel.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text))
			{
				list.Add(base.Doc.GetElement(reference));
			}
			return list;
		}

		public override void TailProcess()
		{
			if (this.tran != null)
			{
				TransactionStatus status = this.tran.GetStatus();
				if ((int)status == 1)
				{
					this.tran.Commit();
				}
				if ((int)status == 5)
				{
					this.tran.RollBack();
				}
			}
		}

		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		private Transaction tran;
	}
}
